package com.ruoyi.common.utils;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.ruoyi.common.utils.property.WeChatProperty;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

public class WeChatUtils {

    private static  String  appId = SpringUtils.getBean(WeChatProperty.class).getAppId();
    private static  String  appSecret =SpringUtils.getBean(WeChatProperty.class).getAppSecret();

    private static  String  appUrl =SpringUtils.getBean(WeChatProperty.class).getAppUrl();
    private static  String  mchId =SpringUtils.getBean(WeChatProperty.class).getMchId();
    private static  String  mchKey =SpringUtils.getBean(WeChatProperty.class).getMchKey();
    private static  String  tradeType =SpringUtils.getBean(WeChatProperty.class).getTradeType();
    private static  String  signType =SpringUtils.getBean(WeChatProperty.class).getSignType();

    private static  String  v2Key =SpringUtils.getBean(WeChatProperty.class).getV2Key();
    private static  String  v3Key =SpringUtils.getBean(WeChatProperty.class).getV3Key();
    private static  String  keyPath =SpringUtils.getBean(WeChatProperty.class).getKeyPath();
    private static  String  privateCert =SpringUtils.getBean(WeChatProperty.class).getPrivateCert();
    private static  String  privateKey =SpringUtils.getBean(WeChatProperty.class).getPrivateKey();
    private static  String  msgData =SpringUtils.getBean(WeChatProperty.class).getMsgData();
    private static  String  createIp =SpringUtils.getBean(WeChatProperty.class).getCreateIp();

    private static  String  gzhAppId =SpringUtils.getBean(WeChatProperty.class).getGzhAppId();
    private static  String  gzhSecret =SpringUtils.getBean(WeChatProperty.class).getGzhSecret();
    private static  String  gzhUrl =SpringUtils.getBean(WeChatProperty.class).getGzhUrl();
    private static  String  wxGzhApi =SpringUtils.getBean(WeChatProperty.class).getWxGzhApi();

    //    public static final String appId = "wx8e06d73b6858a591";
//    public static final String appSecret = "026aa1c2967131176150fa4d8d937868";
//    //素材小程序appid
////    public static final String miniAppId = "wxcd21e824610357ec";//wxd4157f1aeaae2e4c
////    public static final String miniAppSecret = "94cb4fd989638b731c1f0653f94b627f";//f3479b4da5451d2541221ade5a9cc3c5
//   //广峰
////    public static final String miniAppId = "wx2ef9f9f74c049973";
////    public static final String miniAppSecret = "d1ea6f94cb255806f9992a68c3981db1";
//    public static final String mchId = "1629063975";
//    public static final String mchKey = "45C26DA2EB761E291F61B7E002880953";
    private static Logger logger = LoggerFactory.getLogger(WeChatUtils.class);


    /**
     * 小程序-微信用户相关配置
     */
    public static WxMaService getMiniWxMaService() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appId);
        config.setSecret(appSecret);
        config.setMsgDataFormat(msgData);
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(config);
        return wxMaService;
    }

    /**
     * 小程序-微信支付相关配置
     */
    public static WxPayService getMiniWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setTradeType(tradeType);
        payConfig.setSignType(signType);
//        payConfig.setKeyPath(keyPath);
        // payConfig.setApiV3Key("asdqw1345ASD678sezzx123ASE256azQ");//广峰的v3
//        payConfig.setApiVKey(v3Key);
//        payConfig.setPrivateCertPath(privateCert);
//        payConfig.setPrivateKeyPath(privateKey);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    public static WxPayService getWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setTradeType(tradeType);
        payConfig.setSignType(signType);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    public static WxPayService getNativeWxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setKeyPath(keyPath);
        payConfig.setTradeType(tradeType);
        payConfig.setSignType(signType);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

    /**
     * 小程序-微信支付下单
     */
    public static WxPayMpOrderResult weChatPay_mini(String boby, String orderNo, String openId, BigDecimal payMoney, String notifyUrl) {
        try {
            final WxPayService wxPayService = getMiniWxPayService();
            //拉起支付
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                    .body(boby)
                    .outTradeNo(orderNo)
                    .openid(openId)
                    .totalFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .spbillCreateIp(createIp)
                    .notifyUrl(notifyUrl)
                    .build();
            return wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            Assert.isTrue(false, "微信下单失败");

            return null;
        }
    }

    public static WxPayService getMiniWxPayH5Service() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(gzhAppId);
        payConfig.setMchId(mchId);
        payConfig.setMchKey(mchKey);
        payConfig.setTradeType(tradeType);
        payConfig.setSignType(signType);
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }


    public static WxPayMpOrderResult weChatPay_h(String boby, String orderNo, String openId, BigDecimal payMoney, String notifyUrl) {
        try {
            final WxPayService wxPayService = getMiniWxPayH5Service();
            //拉起支付
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                    .body(boby)
                    .outTradeNo(orderNo)
                    .openid(openId)
                    .totalFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .spbillCreateIp(createIp)
                    .notifyUrl(notifyUrl)
                    .tradeType(tradeType)
//                    .productId("12235413214070356458058")
                    .build();
            return wxPayService.createOrder(wxPayUnifiedOrderRequest);
        } catch (WxPayException e) {
            Assert.isTrue(false, "微信下单失败");

            return null;
        }
    }

    public static String weChatPay_na(String boby, String orderNo, BigDecimal payMoney, String notifyUrl) {
        try {
            final WxPayService wxPayService = getNativeWxPayService();

            //拉起支付
            WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest = WxPayUnifiedOrderRequest.newBuilder()
                    .body(boby)
                    .outTradeNo(orderNo)
                    .totalFee(payMoney.multiply(BigDecimal.valueOf(100)).intValue())
                    .spbillCreateIp(createIp)
                    .notifyUrl(notifyUrl)
                    .tradeType(tradeType)
                    .build();
            WxPayNativeOrderResult wxPayNativeOrderResult = wxPayService.createOrder(wxPayUnifiedOrderRequest);
            return wxPayNativeOrderResult.getCodeUrl();
        } catch (WxPayException e) {
            Assert.isTrue(false, "微信下单失败");
            return null;
        }
    }
}
